﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FileUDClassLibrary
{
    /// <summary>
    /// 用于显示文件上传下载的显示组件
    /// </summary>
    public partial class FileUDInfoControl : UserControl
    {
        /// <summary>
        /// 设置当前文件状态
        /// </summary>
        public FileState FileState
        {
            get { return aFileState; }
            set { aFileState = value; }
        }
        
        /// <summary>
        /// 文件类型
        /// <para>影响显示图标</para>
        /// </summary>
        public FileType FileType
        {
            get { return aFileType; }
            set
            {
                aFileType = value;
                SetImg();
            }
        }

        /// <summary>
        /// 设置进度条的最大值
        /// </summary>
        public int Maximum
        {
            get { return PGBar_jd.Maximum; }
            set
            {
                if (value < PGBar_jd.Minimum)
                {
                    throw new Exception("设置值不能小于最小值");
                }
                if (PGBar_jd.Value > value) PGBar_jd.Value = value;
                PGBar_jd.Maximum = value;
            }
        }

        /// <summary>
        /// 设置进度条的最小值
        /// </summary>
        public int Minimum
        {
            get { return PGBar_jd.Minimum; }
            set
            {
                if (value > PGBar_jd.Maximum)
                {
                    throw new Exception("设置值不能大于最大值");
                }
                if (PGBar_jd.Value < value) PGBar_jd.Value = value;
                PGBar_jd.Minimum = value;
            }
        }

        /// <summary>
        /// 设置进度条的当前值
        /// </summary>
        public int Value
        {
            get { return PGBar_jd.Value; }
            set
            {
                if ((value < PGBar_jd.Minimum) || (value > PGBar_jd.Maximum))
                {
                    throw new Exception("当前值不能大于最大值或小于最小值");
                }
                else
                    PGBar_jd.Value = value;
            }
        }

        /// <summary>
        /// 设置是否显示消息
        /// </summary>
        public bool ShowMsg
        {
            get { return tTip_Msg.Active; }
            set
            {
                tTip_Msg.Active = value;
            }
        }

        /// <summary>
        /// 设置显示的消息
        /// </summary>
        public string Message
        {
            set { tTip_Msg.SetToolTip(pan_img, value); }
        }

        /// <summary>
        /// 文件路径
        /// <para>将同时设置文件名和扩展名属性</para>
        /// </summary>
        public string FilePath
        {
            get { return aFilePath; }
            set
            {
                if (!File.Exists(value))
                {
                    throw new Exception("文件不存在");
                }
                aFilePath = value;
                aFileExt = Path.GetExtension(aFilePath).ToLower();
                aFileName = Path.GetFileName(aFilePath).ToLower();
                SetFileType();
            }
        }

        /// <summary>
        /// 文件名
        /// <para>自动转为小写</para>
        /// </summary>
        public string FileName
        {
            get { return aFileName; }
            set
            {
                aFileName = value.ToLower();
            }
        }

        /// <summary>
        /// 文件后缀
        /// <para>自动转为小写</para>
        /// </summary>
        public string FileExt
        {
            get { return aFileExt; }
            set
            {
                aFileExt = value.ToLower();
                SetFileType();
            }
        }

        /// <summary>
        /// 文件大小
        /// <para>自动设置进度条的最大值</para>
        /// </summary>
        public long FileSize
        {
            get { return aFileSize; }
            set
            {
                aFileSize = value;
                Maximum = (int)aFileSize;
            }
        }

        /// <summary>
        /// 服务器保存的路径
        /// </summary>
        public string ServerFilePath
        {
            get { return aServerFilePath; }
            set { aServerFilePath = value; }
        }

        /// <summary>
        /// 文件大小
        /// </summary>
        private long aFileSize = 0;

        /// <summary>
        /// 文件路径
        /// </summary>
        private string aFilePath = "";

        /// <summary>
        /// 文件名
        /// </summary>
        private string aFileName = "";

        /// <summary>
        /// 文件后缀
        /// </summary>
        private string aFileExt = "";

        private string aServerFilePath = "";

        /// <summary>
        /// 文件类型
        /// </summary>
        private FileType aFileType = FileType.UNKNOWN;

        /// <summary>
        /// 文件的状态
        /// </summary>
        private FileState aFileState = FileState.WAIT;

        /// <summary>
        /// 构造函数
        /// </summary>
        public FileUDInfoControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 设置文件图标
        /// </summary>
        private void SetImg()
        {
            switch(aFileType)
            {
                case FileType.EXCEL:
                    {
                        pan_img.BackgroundImage = Properties.Resources.excel;
                        break;
                    }
                case FileType.GIF:
                    {
                        pan_img.BackgroundImage = Properties.Resources.Gif;
                        break;
                    }
                case FileType.JPG:
                    {
                        pan_img.BackgroundImage = Properties.Resources.JPG;
                        break;
                    }
                case FileType.PDF:
                    {
                        pan_img.BackgroundImage = Properties.Resources.PDF;
                        break;
                    }
                case FileType.PNG:
                    {
                        pan_img.BackgroundImage = Properties.Resources.PNG;
                        break;
                    }
                case FileType.TXT:
                    {
                        pan_img.BackgroundImage = Properties.Resources.TXT;
                        break;
                    }
                case FileType.WORD:
                    {
                        pan_img.BackgroundImage = Properties.Resources.word;
                        break;
                    }
                default:
                case FileType.UNKNOWN:
                    {
                        pan_img.BackgroundImage = Properties.Resources.未知;
                        break;
                    }
            }
        }

        private void SetFileType()
        {
            switch (aFileExt)
            {
                case ".doc":
                case ".docx":
                    {
                        aFileType = FileType.WORD;
                        break;
                    }
                case ".xls":
                case ".xlsx":
                    {
                        aFileType = FileType.EXCEL;
                        break;
                    }
                case ".txt":
                    {
                        aFileType = FileType.TXT;
                        break;
                    }
                case ".jpg":
                case ".jpge":
                    {
                        aFileType = FileType.JPG;
                        break;
                    }
                case ".gif":
                    {
                        aFileType = FileType.GIF;
                        break;
                    }
                case ".png":
                    {
                        aFileType = FileType.PNG;
                        break;
                    }
                case ".pdf":
                    {
                        aFileType = FileType.PDF;
                        break;
                    }
                default:
                    {
                        aFileType = FileType.UNKNOWN;
                        break;
                    }
            }
            SetImg();
        }
    }
}
